// 通用表单配置系统
// 支持多种表单类型，易于扩展和维护

import { DEFAULT_MEMBER_TYPES, DEFAULT_SKILL_TYPES } from './memberTypes';

// 表单字段类型枚举
export const FIELD_TYPES = {
  TEXT: 'text',           // 文本输入
  PASSWORD: 'password',   // 密码输入
  TEXTAREA: 'textarea',   // 多行文本
  SELECT: 'select',       // 单选下拉
  MULTI_SELECT: 'multiSelect', // 多选下拉
  RADIO: 'radio',         // 单选按钮
  CHECKBOX: 'checkbox',   // 复选框
  DATE: 'date',           // 日期选择
  NUMBER: 'number',       // 数字输入
  SWITCH: 'switch',       // 开关
  UPLOAD: 'upload',       // 文件上传
};

// 验证规则模板
export const VALIDATION_RULES = {
  required: (message) => ({ required: true, message }),
  email: { type: 'email', message: '请输入有效的邮箱地址' },
  phone: { pattern: /^1[3-9]\d{9}$/, message: '请输入有效的手机号码' },
  url: { type: 'url', message: '请输入有效的URL地址' },
  number: { type: 'number', message: '请输入有效的数字' },
  min: (min) => ({ min, message: `最少输入${min}个字符` }),
  max: (max) => ({ max, message: `最多输入${max}个字符` }),
  pattern: (regex, message) => ({ pattern: regex, message }),
};

// 成员编辑表单配置（只允许修改特定字段）
export const MEMBER_EDIT_FORM_CONFIG = {
  // 表单标题
  title: '编辑成员信息',

  // 表单分组
  groups: [
    {
      key: 'basic',
      title: '基本信息',
      fields: [
        {
          name: 'memberType',
          label: '角色',
          type: FIELD_TYPES.SELECT,
          required: true,
          placeholder: '请选择角色',
          options: DEFAULT_MEMBER_TYPES,
          span: 12,
        },
        {
          name: 'phone',
          label: '手机号',
          type: FIELD_TYPES.TEXT,
          required: false,
          placeholder: '请输入手机号',
          span: 12,
          rules: [VALIDATION_RULES.phone],
        },
        {
          name: 'email',
          label: '邮箱',
          type: FIELD_TYPES.TEXT,
          required: false,
          placeholder: '请输入邮箱',
          span: 12,
          rules: [VALIDATION_RULES.email],
        },
      ],
    },
    {
      key: 'skills',
      title: '技能信息',
      fields: [
        {
          name: 'skills',
          label: '技能专长',
          type: FIELD_TYPES.MULTI_SELECT,
          required: true,
          placeholder: '请选择技能专长',
          options: DEFAULT_SKILL_TYPES,
          span: 24,
        },
        {
          name: 'description',
          label: '个人简介',
          type: FIELD_TYPES.TEXTAREA,
          required: false,
          placeholder: '请输入个人简介',
          span: 24,
          rows: 4,
        },
      ],
    },
  ],
};

// 新增成员表单配置（保留完整字段）
export const MEMBER_FORM_CONFIG = {
  // 表单标题
  title: '成员信息',

  // 表单分组
  groups: [
    {
      key: 'basic',
      title: '基本信息',
      fields: [
        {
          name: 'memberName',
          label: '姓名',
          type: FIELD_TYPES.TEXT,
          required: true,
          placeholder: '请输入姓名',
          span: 12,
        },
        {
          name: 'memberType',
          label: '角色',
          type: FIELD_TYPES.SELECT,
          required: true,
          placeholder: '请选择角色',
          options: DEFAULT_MEMBER_TYPES,
          span: 12,
        },
        {
          name: 'phone',
          label: '手机号',
          type: FIELD_TYPES.TEXT,
          required: false,
          placeholder: '请输入手机号',
          span: 12,
          rules: [VALIDATION_RULES.phone],
        },
        {
          name: 'email',
          label: '邮箱',
          type: FIELD_TYPES.TEXT,
          required: false,
          placeholder: '请输入邮箱',
          span: 12,
          rules: [VALIDATION_RULES.email],
        },
      ],
    },
    {
      key: 'account',
      title: '账号信息',
      fields: [
        {
          name: 'username',
          label: '用户名',
          type: FIELD_TYPES.TEXT,
          required: true,
          placeholder: '请输入用户名',
          span: 12,
          rules: [
            VALIDATION_RULES.pattern(/^[a-zA-Z0-9_]{4,16}$/, '用户名只能包含字母、数字、下划线，长度4-16位'),
          ],
        },
        {
          name: 'password',
          label: '密码',
          type: FIELD_TYPES.PASSWORD,
          required: false,
          placeholder: '请输入密码（留空则保持原密码不变）',
          span: 18,
          rules: [
            ({ getFieldValue }) => ({
              validator(_, value) {
                const allowPasswordEdit = getFieldValue('allowPasswordEdit');
                if (allowPasswordEdit && (!value || value.length < 6)) {
                  return Promise.reject(new Error('密码长度至少为6位'));
                }
                return Promise.resolve();
              },
            }),
          ],
        },
        {
          name: 'allowPasswordEdit',
          label: '修改密码',
          type: FIELD_TYPES.SWITCH,
          required: false,
          placeholder: '',
          span: 6,
          valuePropName: 'checked',
        },
      ],
    },
    {
      key: 'skills',
      title: '技能信息',
      fields: [
        {
          name: 'skills',
          label: '技能专长',
          type: FIELD_TYPES.MULTI_SELECT,
          required: true,
          placeholder: '请选择技能专长',
          options: DEFAULT_SKILL_TYPES,
          span: 24,
        },
        {
          name: 'description',
          label: '个人简介',
          type: FIELD_TYPES.TEXTAREA,
          required: false,
          placeholder: '请输入个人简介',
          span: 24,
          rows: 4,
        },
      ],
    },
  ],
};

// 通用表单配置生成器
export class FormConfigBuilder {
  constructor() {
    this.config = {
      title: '',
      groups: [],
    };
  }

  // 设置表单标题
  setTitle(title) {
    this.config.title = title;
    return this;
  }

  // 添加表单分组
  addGroup(group) {
    this.config.groups.push(group);
    return this;
  }

  // 添加字段到指定分组
  addField(groupKey, field) {
    const group = this.config.groups.find(g => g.key === groupKey);
    if (group) {
      group.fields.push(field);
    }
    return this;
  }

  // 构建配置
  build() {
    return this.config;
  }
}

// 字段配置生成器
export class FieldBuilder {
  constructor(name, label, type) {
    this.field = {
      name,
      label,
      type,
      span: 12,
    };
  }

  // 设置必填
  required(required = true) {
    this.field.required = required;
    return this;
  }

  // 设置占位符
  placeholder(placeholder) {
    this.field.placeholder = placeholder;
    return this;
  }

  // 设置栅格宽度
  span(span) {
    this.field.span = span;
    return this;
  }

  // 设置选项
  options(options) {
    this.field.options = options;
    return this;
  }

  // 设置验证规则
  rules(rules) {
    this.field.rules = rules;
    return this;
  }

  // 设置其他属性
  set(key, value) {
    this.field[key] = value;
    return this;
  }

  // 构建字段
  build() {
    return this.field;
  }
}

// 辅助函数
export const createField = (name, label, type) => new FieldBuilder(name, label, type);
export const createFormConfig = () => new FormConfigBuilder(); 